<template>
	<view class="index">
		<button type="button" @click="handleAuthCode()">登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				appId: 'wxfe50a050f8233773',
				isOk: false,
			};
		},
		onLoad() {

		},
		methods: {
			weixlogin(){
				console.log("授权地址",this.http.httpsLian.wxAuth)
				/* this.request.getRequest(this.http.httpsLian.wxAuth,true)
				.then(res => { 
					console.log("weixlogin："+JSON.stringify(res))
				}).catch(res => {
					console.log(res)
					
				})	 */
				uni.request({
					url: this.http.httpsLian.wxAuth,
					header: {
						'tenant-id': 1,
						"content-type": "application/json"
					},
					success: (res) => {
						console.log("success："+JSON.stringify(res))
					},
					fail: (res) => {
						console.log("fail："+JSON.stringify(res))
						reject("网络出错了");
					},
					complete: () => {
						
					},
				})	
				
			},
			// 登录
			handleLogin(){
				if (!this.isOk) {
					this.handleAuthCode();
				}
			},
			// 授权获取code
			handleAuthCode() {
				let authCode=null; // 截取页面code
				let appId = 'wxfe50a050f8233773'; // 微信公众号的AppId
				let scope = '209c7431983ee5de8846d136524fac71' // 静默授权 用户无感知
				let path = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.appId +
					'&redirect_uri=' + encodeURIComponent('http://www.gxtrwy.cn') + '&response_type=code&scope=' + scope +
					'&state=1#wechat_redirect';
				if (authCode == null || authCode == '' || authCode == undefined) {
					let x = new XMLHttpRequest()
					x.open('GET', path, true)
					x.responseType = 'blob'
					x.onload = function() {
						let url = window.URL.createObjectURL(x.response)
						let a = document.createElement('a')
						a.href = url
						a.click()
						try {
							document.body.removeChild(a) // 下载完成移除元素
							window.URL.revokeObjectURL(url) // 释放掉blob对象
						} catch (e) {}
					}
					x.send()
				} else {
					this.handleOpenId(authCode);
				}
			},
			// 截取 url中的code方法
			handleUrlCode() {
				var url = location.href; //获取打开的公众号的路径
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(url.indexOf('?') + 1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						var items = strs[i].split('=');
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				}
				return theRequest;
			},
			// 发送后端
			handleOpenId(code) {
				console.log("code",code)
				/* uni.request({
					url: 'https://*****', //接口地址。
					data: {
						code: code
					},
					header: {}, //请求头信息
					success: (res) => {
						console.log(res.data);
					}
				}); */

			}
		},
	}
</script>

<style lang="scss">
	.index {
		padding: 20rpx;
	}
</style>
